<template>
  <el-dialog class="dia-style" :title="title" :visible.sync="open" :close-on-click-modal="false" @submit.native.prevent>
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
      <el-col :span="24">
        <el-form-item label="分组名称" prop="type">
          <el-select v-model="formData.type" placeholder="请选择分组类型" :disabled="title == '添加'">
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="字典名称" prop="dictKey">
          <el-input v-model="formData.dictKey" placeholder="请输入字典名称" maxlength="50" />
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="字典值" prop="dictValue">
          <el-input v-model="formData.dictValue" placeholder="请输入字典值" maxlength="50" />
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="字典编码" prop="dictCode">
          <el-input v-model="formData.dictCode" placeholder="请输入字典编码" maxlength="50" />
        </el-form-item>
      </el-col>

      <!-- <el-col :span="24">
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="formData.sort"
            controls-position="right"
          ></el-input-number>
        </el-form-item>
      </el-col> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addDict, updateDict } from '@/api/milkyway/ruleDict'

export default {
  props: {
    title: {
      type: String,
      default() {
        return '标题'
      }
    },
    form: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      // 是否显示弹出层
      open: false,
      // 标题宽度
      labelWidth: '100',
      // 是否显示按钮
      btnHidden: true,
      // 表单项配置

      // 表单校验
      rules: {
        type: [
          {
            required: true,
            message: '请输入字典项分组名称',
            trigger: 'change'
          }
        ],
        dictKey: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
        dictCode: [{ required: true, message: '请输入字典项名称', trigger: 'blur' }],
        dictValue: [{ required: true, message: '请输入字典值', trigger: 'blur' }],
        sort: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      // 类型列表
      typeOptions: [
        {
          label: '映射字典',
          value: '1'
        },
        {
          label: '规则字典',
          value: '2'
        }
      ]
    }
  },
  computed: {
    formData: {
      get() {
        return this.form
      },
      set(val) {}
    }
  },
  watch: {
    form: {
      handler(newValue, oldValue) {},
      deep: true
    }
  },
  mounted() {},

  methods: {
    show() {
      this.open = true
      this.reset()
      this.$nextTick(() => {
        this.$refs.formRef.clearValidate()
      })
    },
    /** 提交按钮 */
    async submitForm() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          if (this.title === '编辑') {
            updateDict({ ...this.formData, isLeaf: 1 }).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.$emit('handleSubmit')
            })
          } else {
            addDict({ ...this.formData, isLeaf: 1 }).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.$emit('handleSubmit')
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.formData = {
        id: undefined,
        groupId: undefined,
        itemVale: undefined,
        itemType: undefined,
        itemLabel: undefined,
        sort: undefined
      }
      this.$refs.formRef.clearValidate()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .el-dialog__body {
    // height: 70% !important;
  }
}
</style>
